//
//  FANSendPictureView.swift
//  Weibo - Swift
//
//  Created by user on 16/4/26.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit

class FANSendPictureView: UICollectionView{
    
    var didAddClosure:(()->())?

    var imageArray:[UIImage] = []
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        let margin = CGFloat(5)
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.minimumInteritemSpacing = margin
        
        layout.minimumLineSpacing = margin
        
        dataSource = self
        
        delegate = self
        
        registerClass(addPictureViewCell.self, forCellWithReuseIdentifier: "picCell")
    }
    
    func addImage(image:UIImage){
        
        imageArray.append(image)
        
        reloadData()
    }
    
    override func layoutSubviews() {
            
        super.layoutSubviews()
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        let margin = CGFloat(5)
        
        let cellWH = (self.frame.size.width - 2*margin)/3
        
        layout.itemSize =  CGSize(width: cellWH, height: cellWH)
        
    }

}

extension FANSendPictureView:UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if imageArray.count == 0 {
            
            self.hidden = true
        }else{
            
            self.hidden = false
        }
        
        return imageArray.count + (imageArray.count == 0 || imageArray.count == 9 ? 0 : 1)
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("picCell", forIndexPath: indexPath) as! addPictureViewCell
        
        if indexPath.item == imageArray.count{
            
            cell.image = nil;
            
        }else{
            
            cell.image = imageArray[indexPath.item]
            
        }
        
        cell.delectedClosure = { [weak self] in
            
            self?.imageArray.removeAtIndex(indexPath.item)
            
            self?.reloadData()
        }
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.item == imageArray.count{
            
            didAddClosure?()
        }
    }
}

class addPictureViewCell:UICollectionViewCell {
    
    var delectedClosure:(()->())?
    
    
    var image:UIImage?{
        
        didSet{
            
            if image == nil{
                
                imageView.image = UIImage(named: "compose_pic_add")
                
                delectedButton.hidden = true
                
            }else{
                
                imageView.image = image
                
                delectedButton.hidden = false

            }
        }
    }
    
    lazy var delectedButton:UIButton = {
    
        let button = UIButton()
        
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "didDelectedButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    
    }()
    
    lazy var imageView:UIImageView = UIImageView()
    
    func didDelectedButton(){
        
        delectedClosure!()
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
//        backgroundColor = randomColor
        
        contentView.addSubview(imageView)
        contentView.addSubview(delectedButton)
        
        delectedButton.snp_makeConstraints { (make) -> Void in
            
            make.top.trailing.equalTo(contentView)
        }
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(contentView)
        }
    }

    
}
